package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.bean.AreaDtoResponse;
import com.example.myapplication.dialogs.AddressDialog;
import com.molichuxing.baselibrary.net.OkHttpManager;
import com.molichuxing.baselibrary.utils.LogUtils;
import com.molichuxing.baselibrary.utils.Tip;
import com.squareup.okhttp.Request;

public class CitySelectActivity extends BaseActivity {

    private TabLayout mTabLayout;
    private TextView tv_address;
    private boolean is_address = false;
    private AreaDtoResponse areaDtoResponse;
    private AddressDialog addressDialog;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_select);

        mTabLayout = findViewById(R.id.act_chapter_tab);
        tv_address = findViewById(R.id.tv_address);

        TabLayout.Tab tab1 = mTabLayout.newTab().setText("省");
        TabLayout.Tab tab2 = mTabLayout.newTab().setText("市");
        TabLayout.Tab tab3 = mTabLayout.newTab().setText("区");
        mTabLayout.addTab(tab1);
        mTabLayout.addTab(tab2);
        mTabLayout.addTab(tab3);
        mTabLayout.removeTab(tab2);

        addressDialog = new AddressDialog(mContext,mActivity);
        initData(false);

//        et_address.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                LogUtils.i("dove","setOnTouchListener-"+is_address);
//                if(is_address){
//                    addressDialog.show(areaDtoResponse, resultCallback);
//                }else{
//                    initData(true);
//                }
//                return true;
//            }
//        });
        tv_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.i("dove","setOnClickListener-"+is_address);
                if(is_address){
                    addressDialog.show(areaDtoResponse, resultCallback);
                }else{
                    initData(true);
                }
            }
        });
    }

    AddressDialog.ResultCallback resultCallback = new AddressDialog.ResultCallback() {
        @Override
        public void onSuccess(String cityNames, String provinceId, String cityId, String areaId) {
            tv_address.setText(cityNames);
            LogUtils.i("dove",provinceId+"-"+cityId+"-"+areaId);
            Tip.showTip(mContext,cityNames+provinceId+cityId+areaId);
        }
    };

    /**
     * 初始化数据
     */
    private void initData(final boolean is_show) {
        OkHttpManager.getInstance().getNet("https://gw.molichuxing.com/base/city", new OkHttpManager.ResultCallback() {
            @Override
            public void onFailed(Request request, Exception e) {
                Tip.showTip(mContext,"加载地区信息失败");
            }
            @Override
            public void onSuccess(String response) {
                try {
                    areaDtoResponse = OkHttpManager.getInstance().getmGson().fromJson(response,AreaDtoResponse.class);
                    if(!"200".equals(areaDtoResponse.getCode())||areaDtoResponse.getData()==null||areaDtoResponse.getData().size()<=0){
                        if(!TextUtils.isEmpty(areaDtoResponse.getMessage())){
                            Tip.showTip(mContext,areaDtoResponse.getMessage());
                        }
                        Tip.showTip(mContext,"加载地区信息失败");
                        is_address = false;
                        return;
                    }
                    is_address = true;
                    setShowProgress(false);
                    if(is_show){
                        addressDialog.show(areaDtoResponse, resultCallback);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    Tip.showTip(mContext,"加载地区信息异常");
                }
            }
        },null);
    }

}
